<?php

include_once '../../__global.php';
include_once SERVER_ROOT_PATH.'/include/ml_function_lib.php';

class aj_islike extends ml_controller{
    private $pid;
    private $aid;
    private $cid;
    private $uid;
    private $isfirst;
    private $callback;
    private $_oRdsAlbumCount;
    private $_oRedisC;

    function initParam() {
        $this->pid = (int)$this->input('pid','G');                // 图片id
        $this->aid = (int)$this->input('aid','G');                    // 相册id
        $this->cid = (int)$this->input('cid','G');                    // 类型id
        $this->uid = (int)$this->input('uid','G');                    // uid
        $this->isfirst = $this->input('isfirst', 'G', 0);
        $this->callback = $this->input('callback','G');
        $this->_oRedisC = new ml_model_rdsCount();
    }
    
    function checkParam(){
        $this->check_referer();
    }
    
    function main() {
        
        //  未登录用户
        if(isset($_COOKIE['LM_UNLOGIN_LIKE'])) {
           $likeids =  explode(',',$_COOKIE['LM_UNLOGIN_LIKE']);
        }else 
            $likeids = array('0'=>-1);
        
        if(in_array($this->pid,$likeids)) {
            $isLike = 1;
        }else{ 
            $isLike = 0;
        }
        
        if ($this->isfirst) {
            $this->_oRdsAlbumCount = new ml_model_album_rdsCount();
            $likeCnt = (int)$this->_oRdsAlbumCount->getAlbumLikeCnt($this->aid);
            $feedCnt = (int)$this->_oRedisC->getAlbumfeedCnt($this->aid);
            $shareCnt = (int)$this->_oRdsAlbumCount->getAlbumWeiboShareCnt($this->aid);
        }   else {
            $likeCnt = (int)$this->_get_single_like();
            $feedCnt = (int)$this->_oRedisC->getPicAlbumfeedCnt($this->aid, $this->pid);
            $oMcShare = new ml_model_album_mcWeiboShare();
            $shareCnt = (int)$oMcShare->getShare($this->cid.'-'.$this->aid.'-'.$this->pid);
        }

        
        $shareCnt = $shareCnt + intval($likeCnt/8);
        
        if(true) { //  成功
            $output_data = array(
                    'likeCnt' => $likeCnt,
                    'sharCnt' => $shareCnt,
                    'isLike'  => $isLike,
                    'feedCnt' => $feedCnt,
            );
            echo 'try{'.$this->callback.'('.json_encode(array('code'=>ML_RCODE_SUCC,'data'=>$output_data)) .');}catch(e){}';
        }else {   //  修改密码失败
            echo 'try{'.$this->callback.'('. json_encode(array('code'=>ML_RCODE_FAIL)).');}catch(e){}';
        }
    }

    private function _get_single_like(){
        $oMeilaPic = new ml_model_admin_dbMeilaPic();
        $rs = $oMeilaPic->getPicByPid($this->pid);
        if(!$rs)
            return false; 
        
        $pic = $oMeilaPic->get_data();
        $like_cnt = (int)$pic['like_visitor'] + (int)$pic['like_user'];
        $oMeilaPoint = new ml_model_admin_dbPoint();
        $rs = $oMeilaPoint->get_by_pointid(array($this->pid));
        $points = $oMeilaPoint->get_data();
        $rids = array();
        if (empty($points)) 
            return gb_function_lib::getVitrulCnt($this->pid, $like_cnt);

        foreach ($points as $point) {
            $pinfo = json_decode($point['point_info'], true);
            if (!empty($pinfo['content_id'])){
                $rids[] = $pinfo['content_id'];
            }
        }
            
        if(empty($rids))
            return gb_function_lib::getVitrulCnt($this->pid, $like_cnt);
        $sum = 0;

        $rs = $this->_oRedisC->getAttitudeCount($rids);
        if(!$rs)
            return false;
        $Ac = $this->_oRedisC->get_data();
        foreach ($Ac as $count) {
            $sum += (int)$count;
        }
        
        return (int)$sum + gb_function_lib::getVitrulCnt($this->pid, $like_cnt);
         
    }

}

new aj_islike();

